<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * tamogatok CRUD model class 
 *
 * @author Gabor Bota <gab.bota at 3pixel.hu>
 */
Class Tamogatok_model extends CI_Model {

    public $tamogato_table;

    function __construct() {
        // Call the Model constructor
        parent::__construct();
        $this->tamogato_table = $this->db->dbprefix('tamogatok');
    }

    /**
     * 
     * 
     * 

     * Update tamogatok by id.
     * 
     * 
     * @return true on success
     */
    function update_tamogato($tamogato_data = array()) {
        // if tamogatok does not exists throw tamogato_not_exists_exception
        if ($this->get_tamogato($tamogato_data['tamogato_id']) == FALSE)
            throw new not_exists_exception("tamogatok");
        // do selection and update data
        $this->db->where('tamogato_id', $tamogato_data['tamogato_id']);
        $this->db->update($this->tamogato_table, $tamogato_data);
    }

    /**
     * 
     * 
     * 
     * Delete tamogatok
     * 
     * 
     * 
     * @param type $id
     */
    function delete($tamogato_id) {
        // delete tamogatok data from tamogato_table
        $this->db->where('tamogato_id', $tamogato_id);
        $this->db->delete($this->tamogato_table);
    }

    /**
     * 
     * Add new tamogatok
     * 
     * @param type $tamogato_data
     */
    function add_tamogato($tamogato_data = array()) {
        // insert tamogato_data
        $this->db->insert($this->tamogato_table, $tamogato_data);
    }

    /**
     * 
     * returns the tamogatok (for pagination)
     * 
     * options:
     *  select 
     *  search
     *  order_by
     *  order_dir
     *  limit
     *  offset
     * 
     * @param type $limit the number of returned tamogatok
     * @param type $offset from this number.
     * @return type 
     */
    function get_tamogatok($options = array()) {

        if (isset($options['select'])) {
            $this->db->select($options['select']);
        }

        if (isset($options['search_like'])) {
            $this->db->like('tamogato_nev', $options['search_like']);
            $this->db->or_like('tamogato_url', $options['search_like']);
        }

        if (isset($options['limit']) && isset($options['offset']))
            $this->db->limit($options['limit'], $options['offset']);

        if (isset($options['order_by'])) {
            if (isset($options['order_dir'])) {
                $this->db->order_by($options['order_by'], $options['order_dir']);
            } else {
                $this->db->order_by($options['order_by']);
            }
        }

        $query = $this->db->get($this->tamogato_table);


        return $query->result();
    }

    /**
     * 
     * Returns the number of tamogatok
     * 
     * 
     * @return type 
     */
    function count() {
        return $this->db->count_all_results($this->tamogato_table);
    }

    /**
     * Gives back tamogatok by id 
     * 
     * @param type $id
     * @return array (tamogato_id, tamogatokname, tamogatok)
     */
    function get_tamogato($id) {
        // constructing active record query
        $query = $this->db->get_where($this->tamogato_table, array('tamogato_id' => $id));
        // do the query
        $result = $query->result();
        if ($result == NULL)
            return NULL;
        return $result[0];
    }

    /**
     *  Ha van az adott pizívióban valami, akkor azt visszadja
     *  ha nincs, akkor nullt- ad vissza.
     * 
     * @param type $position 
     */
    function at_position($position, $id = NULL) {
        if ($id != NULL) {
            $this->db->where('tamogato_id !=', $id);
        }
        $query = $this->db->get_where($this->tamogato_table, array('tamogato_suly' => $position));
        // do the query
        $result = $query->result();
        if ($result == NULL)
            return NULL;
        return $result[0];
    }

}